import Vue from 'vue';

const EventBus = new Vue();

export default EventBus;

export enum EventCommand {
    /** Global Update Data Table Event */
    updateDataTable = 'update-data-table'
}

export function listenEvent(eventName: string, callback: (input: any) => Promise<void>) {
    EventBus.$on(eventName, async (input: any) => {
        EventBus.$emit(`${eventName}-result`, await callback(input));
    });
}

export function sendEvent<T>(eventName: string, input?: T): Promise<any> {
    EventBus.$emit(eventName, input);
    return new Promise<any>((resolve) => {
        EventBus.$on(`${eventName}-result`, (output: any) => resolve(output));
    });
}
